/******************************************************************************
 *                  Shanghai ChipON Micro-Electronic Co.,Ltd
 ******************************************************************************
 *  @File Name        : kf8a100fxx_uart.h
 *  @Author           : ChipON AE/FAE Group
 *  @Date             : 2023-12-19
 *  @HW Version       : KF8A100Fxx
 *  @Lib Version      : V2.0.1
 *  @Description      : This file provides the driver for the uart module
 ******************************************************************************
 *  Copyright (C) by Shanghai ChipON Micro-Electronic Co.,Ltd
 *  All rights reserved.
 *
 *  This software is copyright protected and proprietary to
 *  Shanghai ChipON Micro-Electronic Co.,Ltd.
 *****************************************************************************/
/******************************************************************************
 *  |Date        |Version  |Author       |Description
 ******************************************************************************
 *  |2023-12-25  |V2.0     |Wang Junxiong|New creat
 *****************************************************************************/

#ifndef KF8A100FXX_UART_H
#define KF8A100FXX_UART_H

#ifdef __cplusplus
extern "C" {
#endif

/******************************************************************************
 *                      Include Files
 *****************************************************************************/
#include "kf8a100fxx.h"
/******************************************************************************
 *                      Macro or Inline
 *****************************************************************************/

/**
 * @brief timeout: time limit for flag query, Must not be less than SystemClock/4/BaudRate*10/9
 * You can set it larger if time permits, SystemClock/4/BaudRate*10/9*1.5
 * 0 ~ 65535
 *
 */
#define UART_TIMEOUT_CONFIG (uint16_t)2700u

/**
 * @brief UART module disable/enable
 * 0: disable, 1:enable
 *
 */
#define UART1_ENABLE(x) (SPEN1 = (x))
#define UART2_ENABLE(x) (SPEN2 = (x))
/*****************************************************************************
 *                      Typedef Definitions
 *****************************************************************************/

/**
 * @brief UART Data Length.
 */
typedef enum
{
    /** @brief Serial 8bit mode */
    UART_8BIT_MODE,
    /** @brief Serial 9bit mode */
    UART_9BIT_MODE
} Uart_BitModeType;

/**
 * @brief UART 9-bit Mode enable bit.
 */
typedef enum
{
    /** @brief Serial none-check */
    UART_NONE_MODE = 0u,
    /** @brief Serial odd-check */
    UART_ODD_MODE = 1u,
    /** @brief Serial Even Check */
    UART_EVEN_MODE = 2u
} Uart_ParityModeType;

/**
 * @brief UART baud rate. Based on 16MHz
 */
typedef enum
{
    /** @brief theoretical error: 0.08% */
    UART_BAUDRATE_2400 = 416u,
    /** @brief theoretical error: 0.16% */
    UART_BAUDRATE_4800 = 207u,
    /** @brief theoretical error: 0.16% */
    UART_BAUDRATE_9600 = 103u,
    /** @brief theoretical error: 0.64% */
    UART_BAUDRATE_14400 = 68u,
    /** @brief theoretical error: 0.16% */
    UART_BAUDRATE_19200 = 51u,
    /** @brief theoretical error: 0.16% */
    UART_BAUDRATE_38400 = 25u,
    /** @brief theoretical error: 3.55% */
    UART_BAUDRATE_115200 = 8u
} Uart_BaudRateModeType;

/**
 * @brief UART timeout error type.
 */
typedef enum
{
    UART_OK,
    UART_TIMEOUT
} Uart_StatusType;
/******************************************************************************
 *                      Export Variables
 *****************************************************************************/

/******************************************************************************
 *                      Export Functions
 *****************************************************************************/

/**
 * @brief UART1 initialize
 *
 * @param Prescaler = the baud rate is 16000000/[16*(prescaler+1)]
 * @param Mode 0: 8bit Mode; 1: 9bit Mode
 */
void Uart1_Init(Uart_BaudRateModeType Prescaler, Uart_BitModeType Mode);

/**
 * @brief UART2 initialize
 *
 * @param Prescaler = the baud rate is 16000000/[16*(prescaler+1)]
 * @param Mode 0: 8bit Mode; 1: 9bit Mode
 */
void Uart2_Init(Uart_BaudRateModeType Prescaler, Uart_BitModeType Mode);

/**
 * @brief UART1 Send one byte with parity
 *
 * @param SendData Single byte to be sent
 * @param Mode 0 - none parity mode, 1 - odd parity mode, 2 - even parity mode
 * @return Uart_StatusType
 * 0: UART_OK,
 * 1: UART_TIMEOUT,
 */
Uart_StatusType Uart1_PutChar(uint8_t SendData, Uart_ParityModeType Mode);

/**
 * @brief UART2 Send one byte with parity
 *
 * @param SendData Single byte to be sent
 * @param Mode 0 - none parity mode, 1 - odd parity mode, 2 - even parity mode
 * @return Uart_StatusType
 * 0: UART_OK,
 * 1: UART_TIMEOUT,
 */
Uart_StatusType Uart2_PutChar(uint8_t SendData, Uart_ParityModeType Mode);

/**
 * @brief UART1 Multi-Byte Transmission
 *
 * @attention This function cannot be called within an interrupt service routine.
 *
 * @param Buf data address
 * @param Len data length
 * @param Mode 0 - none parity mode, 1 - odd parity mode, 2 - even parity mode
 * @return Uart_StatusType
 * 0: UART_OK,
 * 1: UART_TIMEOUT,
 */
Uart_StatusType Uart1_Send(const uint8_t *Buf, uint8_t Len, Uart_ParityModeType Mode);

/**
 * @brief UART2 Multi-Byte Transmission
 *
 * @attention This function cannot be called within an interrupt service routine.
 *
 * @param Buf data address
 * @param Len data length
 * @param Mode 0 - none parity mode, 1 - odd parity mode, 2 - even parity mode
 * @return Uart_StatusType
 * 0: UART_OK,
 * 1: UART_TIMEOUT,
 */
Uart_StatusType Uart2_Send(const uint8_t *Buf, uint8_t Len, Uart_ParityModeType Mode);

/**
 * @brief UART1 String Transmission
 *
 * @attention This function cannot be called within an interrupt service routine.
 *
 * @param Buf data address
 * @param Mode 0 - none parity mode, 1 - odd parity mode, 2 - even parity mode
 * @return Uart_StatusType
 * 0: UART_OK,
 * 1: UART_TIMEOUT,
 */
Uart_StatusType Uart1_SendString(const uint8_t *Buf, Uart_ParityModeType Mode);

/**
 * @brief UART2 String Transmission
 *
 * @attention This function cannot be called within an interrupt service routine.
 *
 * @param Buf data address
 * @param Mode 0 - none parity mode, 1 - odd parity mode, 2 - even parity mode
 * @return Uart_StatusType
 * 0: UART_OK,
 * 1: UART_TIMEOUT,
 */
Uart_StatusType Uart2_SendString(const uint8_t *Buf, Uart_ParityModeType Mode);

/**
 * @brief Auto Baud Rate Enable
 *
 */
void Uart1_Auto_BaudRate_Detection_Enable(void);

/**
 * @brief Auto Baud Rate Enable
 *
 */
void Uart2_Auto_BaudRate_Detection_Enable(void);

/**
 * @brief EUBRGL register decremented by one at the end of auto baud rate
 *
 */
void Uart1_Auto_BaudRate_EUBRGL_Sub1(void);

/**
 * @brief EUBRGL register decremented by one at the end of auto baud rate
 *
 */
void Uart2_Auto_BaudRate_EUBRGL_Sub1(void);

/**
 * @brief Receive a byte, and the 9th bit parity correct return data, data in the buffer, error return 0.
 *
 * @param Mode 0 - none parity mode, 1 - odd parity mode, 2 - even parity mode
 * @return uint8_t data, error returns 0
 */
uint8_t Uart1_Rec_Byte(Uart_ParityModeType Mode);

/**
 * @brief Receive a byte, and the 9th bit parity correct return data, data in the buffer, error return 0.
 *
 * @param Mode 0 - none parity mode, 1 - odd parity mode, 2 - even parity mode
 * @return uint8_t data, error returns 0
 */
uint8_t Uart2_Rec_Byte(Uart_ParityModeType Mode);

/**
 * @brief Converts 8bit data to 0 1 string and sends it out
 *
 * @param Data 8bit data
 * @param Mode 0 - none parity mode, 1 - odd parity mode, 2 - even parity mode
 */
void Uart1_Send8Bit2Char(uint8_t Data, Uart_ParityModeType Mode);

/**
 * @brief Converts 8bit data to 0 1 string and sends it out
 *
 * @param Data 8bit data
 * @param Mode 0 - none parity mode, 1 - odd parity mode, 2 - even parity mode
 */
void Uart2_Send8Bit2Char(uint8_t Data, Uart_ParityModeType Mode);

/**
 * @brief Converts 16bit data to 0 1 string and sends it out
 *
 * @param Data 16bit data
 * @param Mode 0 - none parity mode, 1 - odd parity mode, 2 - even parity mode
 */
void Uart1_Send16Bit2Char(uint16_t Data, Uart_ParityModeType Mode);

/**
 * @brief Converts 16bit data to 0 1 string and sends it out
 *
 * @param Data 16bit data
 * @param Mode 0 - none parity mode, 1 - odd parity mode, 2 - even parity mode
 */
void Uart2_Send16Bit2Char(uint16_t Data, Uart_ParityModeType Mode);

/**
 * @brief Uart1 deinitialization
 *
 */
void Uart1_DeInit(void);

/**
 * @brief Uart2 deinitialization
 *
 */
void Uart2_DeInit(void);
#ifdef __cplusplus
}
#endif

#endif
/* EOF */
